<?php
class Model_SupplierContacts extends Model_Template
{

	public function __construct()
	{
		parent::__construct();
		$this->className = 'Model_Table_Supplier';
		
	}
	
	////////////////////////////////////////////////////////return a list of upplier
	public function getAllSupplierContacts()
	{
		//Select all contact query
		$sqlAllSupplierContact = 'SELECT * FROM supplier;';
		
		//Prepare statement
		$allSupplierContact = Controller_Template::$db->prepareEKADBPDO($sqlAllSupplierContact, $this->className, array('supplier_id','name','phone_number'));
		
		//Execute statement
		$allSupplierContact->execute();
		
		
		return $allSupplierContact->fetchAll();
	}
	
	
	///////////////////////////////////////////////////////////create a supplier
	public function createNewSupplierContact($newSC)
	{	
		//Create supplier contact query
		$sqlNewSupplierContact = 'INSERT INTO supplier (name, phone_number) VALUES (?,?);';
	
		//Prepare statement
		$newSupplierContact = Controller_Template::$db->prepareEKADBPDO($sqlNewSupplierContact,$this->className,array('supplier_id','name','phone_number'));
	
		//Execute statement
		$newSupplierContact->execute(array($newSC[':supplier_name'],$newSC[':supplier_phone']));
		
		if($newSupplierContact->rowCount()==1)
		{
			return Controller_Template::$db->lastInsertId();
		}
	}
	
	/////////////////////////////////////////////////////////delete a supplier
	public function deleteSupplierContact($targetContact)
	{
		//Delete contact query
		$sqlDeleteSupplierContact = 'DELETE FROM supplier where supplier_id=?;';
		
		//Prepare statement
		$deleteSupplierContact = Controller_Template::$db->prepareEKADBPDO($sqlDeleteSupplierContact,$this->className,array('supplier_id','name','phone_number'));
		
		//Execute statement
		$deleteSupplierContact->execute(array($targetContact));
		
		return $deleteSupplierContact->rowCount();
	}
	
	/////////////////////////////////////////////////////////update a supplier
	public function updateSupplierContact($targetContact)
	{
		//Update contact query
		$sqlUpdateSupplierContact = 'UPDATE supplier SET name=?,phone_number=? WHERE supplier_id=?;';
		
		//Prepare statement
		$updateSupplierContact = Controller_Template::$db->prepareEKADBPDO($sqlUpdateSupplierContact,$this->className,array('supplier_id','name','phone_number'));
		
		//Execute statement
		$updateSupplierContact->execute(array($targetContact[':supplier_name'],$targetContact[':supplier_phone'],$targetContact[':supplier_id']));
		
		return $updateSupplierContact->rowCount();
	}
	
}